package com.kly.user.chatterService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.excel.util.StringUtils;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.kly.user.db.dao.UserGreetingContentDao;
import com.kly.user.db.entity.UserGreetingContentEntity;
import com.kly.user.dto.chatter.response.UserGreetingContentDto;
import com.kly.user.enums.GreetingStatus;
import com.kly.user.enums.ResultStatus;
import com.kly.user.utils.OtherSnowflake;
import com.kly.user.utils.ServiceUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 招呼语相关处理类
 *
 * @author linqu
 */
@Service("greetingService")
public class GreetingServiceImpl implements GreetingService {

    @Resource
    UserGreetingContentDao userGreetingContentDao;

    @Override
    public Response<List<UserGreetingContentDto>> queryByModelUserCode(String modelUserCode) {
        final List<UserGreetingContentEntity> userGreetingContentEntities = userGreetingContentDao.queryGreetingByModelUserCode(modelUserCode);
        return toDtoList(userGreetingContentEntities);
    }

    private UserGreetingContentDto getUserGreetingContentDto(UserGreetingContentEntity o) {
        final UserGreetingContentDto dto = UserGreetingContentDto.builder().build();
        BeanUtils.copyProperties(o, dto);
        dto.setStatusDesc(GreetingStatus.fromType(dto.getStatus()).getValue());
        return dto;
    }

    @Override
    public Response<List<UserGreetingContentDto>> queryByModelUserCodes(List<String> userCodes) {
        final List<UserGreetingContentEntity> userGreetingContentEntities = userGreetingContentDao.queryGreetingByModelUserCodes(userCodes);
        return toDtoList(userGreetingContentEntities);

    }

    private Response<List<UserGreetingContentDto>> toDtoList(List<UserGreetingContentEntity> userGreetingContentEntities){
        if (CollectionUtils.isNotEmpty(userGreetingContentEntities)) {
            final List<UserGreetingContentDto> greetingContentDtos = userGreetingContentEntities.stream()
                    .map(this::getUserGreetingContentDto)
                    .collect(Collectors.toList());
            return Response.ok(greetingContentDtos);
        }
        return Response.ok();
    }

    @Override
    public Response<List<UserGreetingContentDto>> queryByChatterUserCode(String chatterUserCode) {
        if (StringUtils.isBlank(chatterUserCode)) {
            return Response.ok();
        }

        final List<UserGreetingContentEntity> userGreetingContentEntities = userGreetingContentDao.queryByChatterUserCode(chatterUserCode);
        if (CollectionUtils.isNotEmpty(userGreetingContentEntities)) {
            final List<UserGreetingContentDto> greetingContentDtos = userGreetingContentEntities.stream().map(o -> getUserGreetingContentDto(o)).collect(Collectors.toList());
            return Response.ok(greetingContentDtos);
        }
        return Response.ok();

    }


    @Override
    public Response<Integer> updateGreeting(UserGreetingContentDto greetingContentDto) {

        Map<String, Object> params = ServiceUtils.getParams();
        params.put("greetingCode", greetingContentDto.getGreetingCode());
        List<UserGreetingContentEntity> userGreetingContentEntities = userGreetingContentDao.selectByParams(params);
        if (CollUtil.isNotEmpty(userGreetingContentEntities)) {
            UserGreetingContentEntity greetingContentEntity = new UserGreetingContentEntity();
            BeanUtil.copyProperties(greetingContentDto, greetingContentEntity);
            greetingContentEntity.setId(userGreetingContentEntities.get(0).getId());
            greetingContentEntity.setStatus(GreetingStatus.INIT.getType());
            Integer integer = userGreetingContentDao.updateGreeting(greetingContentEntity);
            return Response.ok(integer);
        } else {
            return Response.error(ResultStatus.GREETING_CODE_NOT_FOUND);
        }
    }

    @Override
    public Response<Integer> saveGreeting(UserGreetingContentDto greetingContentDto) {
        UserGreetingContentEntity greetingContentEntity = new UserGreetingContentEntity();
        BeanUtil.copyProperties(greetingContentDto, greetingContentEntity);
        String greetingCode = String.valueOf(OtherSnowflake.getInstance().nextId());
        greetingContentEntity.setGreetingCode(greetingCode);
        greetingContentEntity.setStatus(GreetingStatus.INIT.getType());
        Integer integer = userGreetingContentDao.saveGreeting(greetingContentEntity);
        return Response.ok(integer);
    }

    @Override
    public Response<Integer> delGreeting(UserGreetingContentDto greetingContentDto) {
        Map<String, Object> params = ServiceUtils.getParams();
        params.put("greetingCode", greetingContentDto.getGreetingCode());
        List<UserGreetingContentEntity> userGreetingContentEntities = userGreetingContentDao.selectByParams(params);
        if (CollUtil.isNotEmpty(userGreetingContentEntities)) {
            int delGreeting = userGreetingContentDao.delGreeting(userGreetingContentEntities.get(0).getId());
            return Response.ok(delGreeting);
        } else {
            return Response.error(ResultStatus.GREETING_CODE_NOT_FOUND);
        }
    }

}
